

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.cef.CefApp;
import org.cef.CefClient;
import org.cef.CefSettings;
import org.cef.browser.CefBrowser;
import org.cef.browser.CefMessageRouter;
import org.cef.callback.CefAuthCallback;
import org.cef.callback.CefGeolocationCallback;
import org.cef.callback.CefQueryCallback;
import org.cef.callback.CefRequestCallback;
import org.cef.handler.CefDisplayHandler;
import org.cef.handler.CefFocusHandler;
import org.cef.handler.CefGeolocationHandler;
import org.cef.handler.CefLifeSpanHandler;
import org.cef.handler.CefLoadHandlerAdapter;
import org.cef.handler.CefMessageRouterHandler;
import org.cef.handler.CefRequestHandler;
import org.cef.handler.CefResourceHandler;
import org.cef.handler.CefFocusHandler.FocusSource;
import org.cef.handler.CefLoadHandler.ErrorCode;
import org.cef.handler.CefRequestHandler.TerminationStatus;
import org.cef.misc.BoolRef;
import org.cef.misc.StringRef;
import org.cef.network.CefRequest;
import org.cef.network.CefWebPluginInfo;


public class AeBrowser extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 2662964071263830659L;
    private static AeBrowser instance;
    private static final int BROWSER_WIDTH = 900;
    private static final int BROWSER_HEIGHT = 600;
    private static final boolean enableOSR = false;
    private final CefClient cefClient;
    private final CefBrowser cefBrowser;
    private boolean hasError = false;
    private static final String ABOUT_BLANK = "C:\\Users\\zhizhong\\Desktop\\resources\\binding.html";//"http://www.baidu.com";

    private AeBrowser() {
        CefSettings settings = new CefSettings();
        settings.windowless_rendering_enabled = enableOSR;
        settings.log_severity = CefSettings.LogSeverity.LOGSEVERITY_ERROR;

        CefApp app = CefApp.getInstance(settings);
        cefClient = app.createClient();

        cefClient.addRequestHandler(new CefRequestHandler() {

            @Override
            public void onResourceRedirect(CefBrowser arg0, CefRequest arg1, StringRef arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onRenderProcessTerminated(CefBrowser arg0, TerminationStatus arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public boolean onQuotaRequest(CefBrowser arg0, String arg1, long arg2,
                    CefRequestCallback arg3) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void onProtocolExecution(CefBrowser arg0, String arg1, BoolRef arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPluginCrashed(CefBrowser arg0, String arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public boolean onCertificateError(CefBrowser arg0, ErrorCode arg1, String arg2,
                    CefRequestCallback arg3) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean onBeforeResourceLoad(CefBrowser arg0, CefRequest arg1) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean onBeforePluginLoad(CefBrowser arg0, String arg1, String arg2,
                    CefWebPluginInfo arg3) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean onBeforeBrowse(CefBrowser arg0, CefRequest arg1, boolean arg2) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public CefResourceHandler getResourceHandler(CefBrowser arg0, CefRequest arg1) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public boolean getAuthCredentials(CefBrowser arg0, boolean arg1, String arg2, int arg3,
                    String arg4, String arg5, CefAuthCallback arg6) {
                // TODO Auto-generated method stub
                return false;
            }
        });

        cefClient.addMessageRouter(CefMessageRouter.create(new CefMessageRouterHandler() {

            
            @Override
            public void setNativeRef(String arg0, long arg1) {
                
            }
            
            @Override
            public long getNativeRef(String arg0) {
                return 0;
            }
            
            @Override
            public void onQueryCanceled(CefBrowser arg0, long arg1) {
                
            }
            
            @Override
            public boolean onQuery(CefBrowser arg0, long arg1, String arg2, boolean arg3,
                    CefQueryCallback arg4) {
                
                System.out.println(arg1+arg2+arg3);
                
//                arg4.success("aaaaa");
                arg4.failure(1, "sddd");
                return true;
            }
        }));
        
        cefClient.addLifeSpanHandler(new CefLifeSpanHandler() {
            
            @Override
            public boolean runModal(CefBrowser arg0) {
                System.out.println("runModal");
                return false;
            }
            
            @Override
            public boolean onBeforePopup(CefBrowser arg0, String arg1, String arg2) {
                System.out.println("onBeforePopup"+arg1+"--------------"+arg2);
                return false;
            }
            
            @Override
            public void onBeforeClose(CefBrowser arg0) {
                System.out.println("onBeforeClose");
            }
            
            @Override
            public void onAfterCreated(CefBrowser arg0) {
                System.out.println("onAfterCreated");
            }
            
            @Override
            public boolean doClose(CefBrowser arg0) {
                System.out.println("doClose");
                return false;
            }
        });
        
        
        cefClient.addDisplayHandler(new CefDisplayHandler() {

            @Override
            public boolean onTooltip(CefBrowser arg0, String arg1) {
                return false;
            }

            @Override
            public void onTitleChange(CefBrowser arg0, String arg1) {

            }

            @Override
            public void onStatusMessage(CefBrowser arg0, String arg1) {

            }

            @Override
            public boolean onConsoleMessage(CefBrowser arg0, String arg1, String arg2, int arg3) {
                return false;
            }

            @Override
            public void onAddressChange(CefBrowser arg0, String arg1) {
                System.out.println(arg1);
            }
        });


        cefClient.addLoadHandler(new CefLoadHandlerAdapter() {

            @Override
            public void onLoadingStateChange(CefBrowser browser, boolean isLoading,
                    boolean canGoBack, boolean canGoForward) {

                SwingUtilities.updateComponentTreeUI(browser.getUIComponent());
                if (!isLoading && hasError) {
                    browser.loadString(ABOUT_BLANK, ABOUT_BLANK);
                    hasError = false;
                }
            }

            @Override
            public void onLoadError(CefBrowser browser, int frameIdentifer, ErrorCode errorCode,
                    String errorText, String failedUrl) {
                if (errorCode != ErrorCode.ERR_NONE && errorCode != ErrorCode.ERR_ABORTED) {
                    hasError = true;
                    browser.stopLoad();
                }
            }
        });



        cefBrowser = cefClient.createBrowser(ABOUT_BLANK, enableOSR, true);

        Component component = cefBrowser.getUIComponent();
        getContentPane().add(component, BorderLayout.CENTER);


        setSize(BROWSER_WIDTH, BROWSER_HEIGHT);
        setVisible(true);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                AeBrowser.this.setVisible(false);
            }
        });
    }



    public void loadURL(String htmlContent) {
        cefBrowser.loadURL(htmlContent);
    }

    public static synchronized AeBrowser getInstance() {
        if (instance == null) {
            instance = new AeBrowser();
        }
        return instance;
    }
    
    
   
}
